import os
import uvicorn
from app.core.logger import log
from app.core.config import settings
from app.module.router import create_app
from app.module.reminder.scheduler import create_scheduler


# 只负责创建应用；数据库与调度器由 router.create_app() 内部的启动钩子管理
app = create_app()

# 强制在 main 中启动提醒调度器（不考虑关闭）
try:
    scheduler = create_scheduler()
    scheduler.start()
    log.info("Reminder scheduler started from main.py")
except Exception as e:
    log.error(f"Failed to start reminder scheduler: {e}")

if __name__ == "__main__":

    log.info(settings.print_config(show_all=True))

    """使用 Uvicorn 启动 FastAPI 应用"""

    host = os.environ.get("HOST", "127.0.0.1")
    port = int(os.environ.get("PORT", "8000"))
    reload = os.environ.get("RELOAD", "true").lower() in {"1", "true", "yes", "on"}
    log_level = os.environ.get("LOG_LEVEL", "info")

    # 指向当前包内的 FastAPI 实例 app
    uvicorn.run(
        "app.main:app",
        host=host,
        port=port,
        reload=reload,
        log_level=log_level,
    )
    log.info("应用程序启动完成")

